<?php

namespace App\Http\Middleware;

use App\severces\JwtSeverce;
use Closure;
use Illuminate\Http\Request;

class CheckApi
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        try {
            $token = $request->header('token');

            if(empty($token)){
                abort(102,'token必须');
            }

            $uid = JwtSeverce::jwtDecode($token)['data'];

            $request->uid = $uid;

            return $next($request);

        }catch (\Exception $e){
            exit(json_encode([
                'error_code'=>101,
                'data'=>null,
                'msg'=>$e->getMessage()
            ],256));
        }

    }
}
